package test.account;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import hyl.core.db.MyDB;

public class AccountDao {
	protected MyDB _db;
	protected static final Logger logger = LoggerFactory.getLogger(AccountDao.class);
	protected int pagesize = 100;

	public AccountDao(MyDB db) {
		_db = db;
	}

	public void commit() {
		_db.commit();
	}	

	

	/**
	 * 重新创建账户 String 用户,String 科目,BigDecimal 金额, String 备注
	 */
	public void replace账户(String 用户, String 科目, BigDecimal 金额, String 备注) {
		String sql = "replace INTO act_finance(ctime,etime,je,userid,subj,remark) VALUES(NOW(),NOW(),?,?,?,?)";
		Object[] ps = new Object[4];
		ps[0] = 金额;
		ps[1] = 用户;
		ps[2] = 科目;
		ps[3] = 备注;
		_db.executeUpdate(sql, ps);
	}

	// 失效账户
	public void invalid账户(String 用户, String 科目) {
		String sql = "update act_finance set invalidtime=now() where userid=? and subj=? ";
		Object[] ps = new Object[2];
		ps[0] = 用户;
		ps[1] = 科目;
		_db.executeUpdate(sql, ps);
	}

	// 插入账本
	public void insert账本(String 流水号, String 用户, String 科目, BigDecimal 金额, String 备注) {
		// 账户调整
		String sql = "call p_addAccount(?,?,?,?)";
		Object[] ps = new Object[4];	
		ps[0] = 用户;
		ps[1] = 科目;
		ps[2] = 金额;
		ps[3] = null;
		_db.excuteFun(sql, ps);
		// 记凭证
		sql = "insert into book_account (mon,ctime,flowid,userid,subj,je,remark) values(EXTRACT(YEAR_MONTH FROM now()),now(),?,?,?,?,?)";
		ps = new Object[5];
		ps[0] = 流水号;
		ps[1] = 用户;
		ps[2] = 科目;
		ps[3] = 金额;
		ps[4] = 备注;
		_db.executeUpdate(sql, ps);
	}
	

	// 查询我的账户
	public List<List<Object>> select账户(Map<String, Object> ps) {
		StringBuilder sql = new StringBuilder("select * from act_finance where 1=1 ");
		List<Object> objs = new ArrayList<Object>();
		if (ps == null || ps.isEmpty()) {
			sql.append("limit ").append(pagesize);
		} else {
			Object p1 = ps.get("userid");
			Object p2 = ps.get("subj");
			if (p1 != null) {
				sql.append("and userid = ?");
				objs.add(p1);
			}
			if (p2 != null) {
				sql.append("and subj = ?");
				objs.add(p2);
			}
			if (ps.containsKey("rs_index")) {
				sql.append("limit ?,").append(pagesize);
				objs.add(ps.get("rs_index"));
			} else {
				sql.append("limit ").append(pagesize);
			}
		}
		List<List<Object>> d = _db.queryList2D(sql.toString(), objs.toArray());
		return d;
	}

	// 查看账本
	public List<List<Object>> select账本(Map<String, Object> ps) {
		StringBuilder sql = new StringBuilder("select * from book_account where 1=1 ");
		List<Object> objs = new ArrayList<Object>();
		if (ps == null || ps.isEmpty()) {
			sql.append("limit ").append(pagesize);
		} else {
			Object p1 = ps.get("userid");
			Object p2 = ps.get("subj");
			Object p3 = ps.get("mon");
			if (p1 != null) {
				sql.append("and userid = ?");
				objs.add(p1);
			}
			if (p2 != null) {
				sql.append("and subj = ?");
				objs.add(p2);
			}
			if (p3 != null) {
				sql.append("and mon = ?");
				objs.add(p3);
			}
			if (ps.containsKey("rs_index")) {
				sql.append("limit ?,").append(pagesize);
				objs.add(ps.get("rs_index"));
			} else {
				sql.append("limit ").append(pagesize);
			}
		}
		List<List<Object>> d = _db.queryList2D(sql.toString(), objs.toArray());
		return d;
	}


}
